#include <opencv2/opencv.hpp>
#include <vector>
#include <cmath>
using namespace std;
using namespace cv;

template <typename T>
struct M_Point
{
    M_Point(T x, T y):x_(x), y_(y) {}
    T x_;
    T y_;
};

template <typename T>
int findFirstPoint(std::vector<M_Point<T>>& data)
{
    int idx = 0;
    T minY = data[0].y_;
    for (int i = 1; i < data.size(); ++i) {
        if (data[i].y_ < minY) {
            minY = data[i].y_;
            idx = i;
        } else if (data[i].y_ == minY && data[i].x_ < data[idx].x_) {
            idx = i;
        }
    }

    return idx;
}

int sortPoints(std::vector<M_Point>& in, std::vector<int>& Index)
{
    int firstIdx = findFirstPoint(in);
    Index.clear();
    Index.push_back(firstIdx);

    std::vector<double> angle(in.size(), -1.f);

    for (int i = 0; i < in.size(); ++i) {
        if (i == firstIdx) { continue; }
        if(fabs(in[firstIdx].x_ - in[i].x_) < 1e-4) {
            angle[0] = M_PI_2;
        }
        angle[i] = acos();
    }

    return 0;
}


int main(int argc, char *argv[])
{
    int width = 640, height = 480;
    if(argc==3) {
        width = atoi(argv[1]);
        height = atoi(argv[2]);
    }
    cv::Mat image(height,width,CV_8UC3,cv::Scalar(255,255,255));

    cv::RNG rng;
    std::vector<M_Point<float>> data;
    for (int i = 0; i < 100; ++i) {
        cv::Point2f point(rng.uniform(0.1f, static_cast<float>(width)-0.1), rng.uniform(0.1f,static_cast<float>(height)-0.1));
        data.push_back(M_Point<float>(point.x, point.y));
        cv::circle(image,point,1,Scalar(0,0,255));
    }

    cv::imshow("random",image);
    cv::waitKey();

    return 0;
}
